(function (NS, $, undefined ) {

var _pctrl,_appctrl;

    NS.SPApp.CommonHeaderController= Ember.Controller.extend({
	  commonHeaderVar: "CommonHeader from CommonHeaderController" ,
	  getHeaders : function ()
	  {
		 return "computed header details from json";
	  }.property('getHeaders')
	});

    NS.SPApp.TopNavigationController= Ember.Controller.extend({
	  commonNavVar: "CommonNav from TopNavigationController" ,
	});

    NS.SPApp.CommonFooterController= Ember.Controller.extend({
	  commonFootVar: "CommonFooter from CommonFooterController" ,
	});
    
    NS.SPApp.ProductsController= Ember.ArrayController.extend({
	  commonProductsVar: "Products from ProductsController" 
	});
    
	
    NS.SPApp.ProductsRoute = NS.SPApp.BaseRoute.extend({
		init: function(){
			this._super();
			console.log('init is  called........');
			_pctrl = this.controllerFor('products');
			_appctrl = this.controllerFor('application');
		},
		model: function() {
		    console.log('ProductsRoute model called........');
			return NS.SPApp.JSONValueObject.getAll();
		},
		renderTemplate: function() {
			console.log('renderTemplate called........');
			this.render('header',{ outlet: 'commonheader' , controller: 'commonHeader'});
			this.render('footer',{ outlet: 'commonfooter' , controller: 'products'});
			this.render('topnav',{ outlet: 'topnav' , controller: 'topNavigation'});
			this.render('sidenav',{ outlet: 'sidenav' , controller: 'sideNavigation'});
			this.render('contentbody',{ outlet: 'contentbody' , controller: 'contentBody'});		
		}  
    });


    NS.SPApp.CustomRoute = Ember.Route.extend({
		renderTemplate: function() {
			this.render('custom',{ controller: 'commonHeader' });
		}  
    });
    
    NS.SPApp.HeaderView = Ember.View.extend({
	  templateName: 'header',
	  name : 'name variable from HeaderView',
	  message : 'message from HeaderView '
    });

    NS.SPApp.FooterView = Ember.View.extend({
	  templateName: 'footer',
	  name : 'name variable from FooterView',
	  message : 'message from FooterView '
    });

     
	NS.SPApp.JSONValueObject = Ember.Object.extend();	
	NS.SPApp.JSONValueObject.reopenClass({
	  allGeonamesdata: [],
	  getAll: function(){
		this.allGeonamesdata = [];
		$.ajax({
		  url: 'http://ws.geonames.org/searchJSON?&country=US',
		  dataType: 'jsonp',
		  context: this,
		  success: function(response){
		  //debugger;
			_pctrl.set('commonProductsVar','changed on the fly.......');
			console.log("currentpath----->  " + _appctrl.get('currentPath'));
			response.geonames.forEach(function(geonamesdata){
			  this.allGeonamesdata.addObject(geonamesdata)
			}, this)
		  }
		});
		 
		return this.allGeonamesdata;
	  }
	}); 


    })(com.ui = com.ui || {}, $);